﻿/*
 * 验收入库
 * 参数   交易码：trancode=61117
 * 
 */

package org.ccams.tran;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.ccams.domain.TMateriel;
import org.ccams.domain.TMaterielStock;
import org.ccams.domain.TpMovbackD;
import org.ccams.web.WebBeanFactory;
import org.json.JSONArray;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class Tran61117 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Long num = Long.parseLong(request.getParameter("num"));
		// String uname = request.getParameter("uname");
		int uid = Integer.parseInt(request.getParameter("uid"));
		int back_uid = Integer.parseInt(request.getParameter("back_uid"));
//		String backuname = request.getParameter("backuname");

		String params = (String) request.getParameter("detail");
		JSONArray detailParams = new JSONArray(params);

		Short state = Short.parseShort(request.getParameter("state"));// 取得审核状态

		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";
		int k = 0;
		DataSourceTransactionManager txm = (DataSourceTransactionManager) WebBeanFactory.getBean(
				"transactionManager", request);
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		TransactionStatus status = txm.getTransaction(def);
		try {
			TpMovbackD tmd = new TpMovbackD();
			tmd.setNum(num);
			tmd.setState(state);
			k = getCcamsService().getTpmovbackdDao().updateInspection(tmd);// 更新审核信息
			if (state == 3 && k >= 1) {// state == 3 为通过审核
				for (int i = 0; i < detailParams.length(); i++) {
					org.json.JSONObject detail = detailParams.optJSONObject(i);
					TMaterielStock tms = new TMaterielStock();
					// tms.setUname(uname);
					tms.setUid(uid);
					tms.setIdt_materiel(detail.getLong("idt_Materiel"));
					tms.setAmount(0 - detail.getLong("amount"));
					k = getCcamsService().getTmaterielstockDao().updateStockAmount(tms);// 减去调出部门物料数量

					// tms.setMaterielName(detail.getString("materielName"));
//					tms.setIdt_materiel(detail.getLong("idt_Materiel"));
					// tms.setUname(backuname);
					tms.setUid(back_uid);
					tms.setAmount(detail.getLong("amount"));
					int idt = getCcamsService().getTmaterielstockDao().getIdtStock(tms);
					if (idt == 0) {// 接收部门库存中没有此物料记录信息,执行添加
						TMateriel tm = getCcamsService().getTmaterielDao().getTMaterielById(detail.getLong("idt_Materiel"));

//						tms.setIdt_materiel(tm.getIdt_materiel());
						tms.setMaterielName(tm.getMaterielname());
						tms.setAttrib1(tm.getAttrib1());
						tms.setAttrib2(tm.getAttrib2());
						tms.setAttrib3(tm.getAttrib3());
						tms.setAttrib4(tm.getAttrib4());
						tms.setAttrib5(tm.getAttrib5());
						k = getCcamsService().getTmaterielstockDao().insertTMaterielStock(tms);
					} else {
						k = getCcamsService().getTmaterielstockDao().updateStockAmount(tms);// 更新验收入库数量
					}
				}
			}/*
			 * else if(state == 1 && k >= 1){//state == 1 为已作废 for(int
			 * i=0;i<detailParams.length();i++){ org.json.JSONObject detail =
			 * detailParams.optJSONObject(i); TMaterielStock tms = new
			 * TMaterielStock(); tms.setUname(uname);
			 * tms.setMaterielName(detail.getString("materielName"));
			 * tms.setAmount(detail.getLong("amount")); k =
			 * getCcamsService().getTmaterielstockDao
			 * ().updateStockAmount(tms);//减去调出部门物料数量 } }
			 */
		} catch (Exception ex) {
			txm.rollback(status);
			ex.printStackTrace();
		} finally {
			txm.commit(status);
		}
		if (k >= 1) {
			success = true;
			message = "审核成功";
		} else {
			success = false;
			message = "审核失败";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
